import Sprite from "./sprite"
import Map from "./map"
const screenWidth = window.innerWidth
const screenHeight = window.innerHeight
const __ = {
  poolDic: Symbol('poolDic')
}


const selectView = new Sprite('images/resource/tex/game/selectView.bmp',0,0,0,0,9*16*devicePixelRatio, 4*16*devicePixelRatio,4*16*devicePixelRatio,2*16*devicePixelRatio,'')
const startArrow = new Sprite('images/resource/tex/ui/arrow/0.bmp',0,0,0,0,screenWidth/10/2,screenHeight/10/1.5,11*16*devicePixelRatio,3*16*devicePixelRatio,'arrow')
const startMovie = new Sprite('images/resource/tex/ui/startMovie/4.png',0,0,0,0,9*16*devicePixelRatio, 6*16*devicePixelRatio,4*16*devicePixelRatio,1*16*devicePixelRatio,'startMovie')
const worldMap = new Map('images/resource/map/sqg_bai_cao_di',0,0,0,0,0,0,0,0,'sqg_bai_cao_di')
const mapConverts = []
const hero = new Sprite('images/resource/anim/hero/liu_qing_feng/lqf.png',0,0,0,0,0,0,0,0,'liu_qing_feng')
const btnL = new Sprite('images/resource/tex/ui/btnL.png',0,0,0,0,screenWidth/4,screenWidth/4,screenWidth/10*0.6,screenHeight/10*4,'')
const btnR = new Sprite('images/resource/tex/ui/btnR.png',0,0,0,0,screenWidth/3,screenWidth/3,screenWidth/10*7,screenHeight/10*3,'')
const mapMsg = {},sayMsg = {}
const movies = []
const npcs = []
/**
 * 简易的对象池实现
 * 用于对象的存贮和重复使用
 * 可以有效减少对象创建开销和避免频繁的垃圾回收
 * 提高游戏性能
 */
export default class Pool {
  constructor() {
    this[__.poolDic] = {}
    this.svSprites = [selectView,startArrow]
    this.controllSprites = [btnL,btnR]
    this.startMovie = startMovie
    this.worldMap = worldMap
    this.mapConverts = mapConverts
    this.hero = hero
    this.sayMsg = sayMsg
    this.mapMsg = mapMsg
    this.msgBox = []
    this.movies = movies
    this.npcs = npcs
  }

  /**
   * 根据对象标识符
   * 获取对应的对象池
   */
  getPoolBySign(name) {
    return this[__.poolDic][name] || (this[__.poolDic][name] = [])
  }

  /**
   * 根据传入的对象标识符，查询对象池
   * 对象池为空创建新的类，否则从对象池中取
   */
  getItemByClass(name, className) {
    const pool = this.getPoolBySign(name)

    const result = (pool.length
      ? pool.shift()
      : new className())

    return result
  }

  /**
   * 将对象回收到对象池
   * 方便后续继续使用
   */
  recover(name, instance) {
    this.getPoolBySign(name).push(instance)
  }
}
